let app = getApp();

//内网穿透工具介绍:
// https://open-doc.dingtalk.com/microapp/debug/ucof2g
//替换成开发者后台设置的安全域名
//console.log(app.globalData);


let globalData = app.globalData;
let domain =  globalData.domain;
let url = domain + '/ding/login';
function ddRequest(options, fnSuccess, fnFail, fnComplete) {
  dd.httpRequest({
    url: options.url,
    method: options.method || 'POST',
    data: options.data,
    dataType: options.dataType || 'json',
    success: (res) => {
      if (fnSuccess) {
        fnSuccess(res);
      }
    },
    fail: (res) => {
      if (fnFail) {
        fnFail(res);
      }
      console.log("httpRequestFail---", res)
      dd.alert({ content: JSON.stringify(res) });
    },
    complete: (res) => {
      if (fnComplete) {
        fnComplete(res);
      }
      dd.hideLoading();
    }
  });
}

Page({
  data: {
    authCode: '',
    userId: '',
    userName: '',
    hideList: true,
    elafs_wechat_url: ''
  },
  onLoad() {
    const v = this;
    //dd.alert({content: "step1"});
    dd.showLoading();
    dd.getAuthCode({
      success: (resp) => {
        ddRequest({
          url: url,
          data: {
            authCode: resp.authCode,
            site: globalData.domain_name
          }
        }, function (res) {
          console.log(res.data);
          const data = res.data;
          if (data && data.success) {
            v.setData({ elafs_wechat_url: data.result.direct_url });
          } else {
            dd.alert({ content: data.message });
          }
          console.log(v.data);
        });

      },
      fail: (err) => {
        // dd.alert({content: "step3"});
        dd.alert({
          content: JSON.stringify(err)
        })
      }

    });

  }
})